package com.cardbaobao.cardbabyclient.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.callback.RecycleItemClickedCallback;
import com.cardbaobao.cardbabyclient.ui.fragment.CreditFragment;
import com.cardbaobao.cardbabyclient.model.CreditCardIndexLayout;
import com.cardbaobao.cardbabyclient.utils.ImageLoaderUtil;
import com.cardbaobao.cardbabyclient.utils.URLUtil;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Jiangzx on 14:23.
 */

public class HotRecQualityLifeDelegateAdapter extends DelegateAdapter.Adapter<RecyclerView.ViewHolder>{
    private LayoutHelper mLayoutHelper;
    private List<CreditCardIndexLayout.QualityLife> data;
    private RecycleItemClickedCallback clickedCallback;
    public void setRecycleItemClickedCallback(RecycleItemClickedCallback clickedCallback){
        this.clickedCallback=clickedCallback;
    }
    public HotRecQualityLifeDelegateAdapter(LayoutHelper layoutHelper,
                                            List<CreditCardIndexLayout.QualityLife> data){
        this.mLayoutHelper=layoutHelper;
        this.data=data;
    }
    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).
                inflate(R.layout.item_image_text_text_no_margin,parent,false);
        return new QualityLifeHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if(holder instanceof QualityLifeHolder){
            QualityLifeHolder lifeHolder= (QualityLifeHolder) holder;
            int modeSpace=position%4;
            switch (modeSpace){
                case 0:
                    lifeHolder.itemView.setBackgroundResource(R.mipmap.img_line3_left);
                    break;
                case 1:
                    lifeHolder.itemView.setBackgroundResource(R.mipmap.img_line4_left);
                    break;
                case 2:
                    lifeHolder.itemView.setBackgroundResource(R.mipmap.img_line1_right);
                    break;
                case 3:
                    lifeHolder.itemView.setBackgroundResource(R.mipmap.img_line2_left);
                    break;
            }
            CreditCardIndexLayout.QualityLife qualityLife=data.get(position);
            ImageLoaderUtil.displayImageWithPlaceHolder(URLUtil.CBB_IMG_URL+qualityLife.getPicture_path(),
                    lifeHolder.civ,
                    R.mipmap.img_default_square);
            lifeHolder.tvTitle.setText(qualityLife.getName());
            lifeHolder.tvContent.setText(qualityLife.getMarketing_one());
            lifeHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(clickedCallback!=null){
                        clickedCallback.onRecycleItemClicked(position);
                    }
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        return CreditFragment.TYPE_QUALITY_LIFE;
    }

    @Override
    public int getItemCount() {
        return data==null?0:data.size();
    }

    //品质生活
    private class QualityLifeHolder extends RecyclerView.ViewHolder{
        private CircleImageView civ;
        private TextView tvTitle;
        private TextView tvContent;
        private QualityLifeHolder(View itemView) {
            super(itemView);
            civ= (CircleImageView) itemView.findViewById(R.id.civ);
            tvTitle= (TextView) itemView.findViewById(R.id.tv_title);
            tvContent= (TextView) itemView.findViewById(R.id.tv_content);
        }
    }
}
